#!/usr/bin/python

import ctypes

def getlib(string="/usr/loca/bin/libkvs.so"):
    
    lib = ctypes.CDLL(string)
    return lib

def kv_init(lib):
    
    string = raw_input("config_file:")
    config_file_str = ctypes.c_char_p()
    config_file_str.value = string
    
    lib.kv_init(config_file_str)

def kv_put(lib, key, value):
    key_len = len(key)
    val_len = len(value)
    
    key_str = ctypes.addressof((ctypes.c_char * (key_len + 1))())
    val_str = ctypes.addressof((ctypes.c_char * (val_len + 1))())

    key_str = key
    val_str = value
    
    lib.kv_put(key_str, key_len, val_str, val_len)

def kv_get(lib, key, bufsize):
    key_len = len(key)
    key_str = ctypes.addressof((ctypes.c_char * (key_len + 1))())
    key_str = key
    buf = ctypes.create_string_buffer(bufsize)
    lib.kv_get(key_str, key_len, ctypes.byref(buf), bufsize)
    value = "%s" % buf.value
    return value

def kv_delete(lib, key):
    key_len = len(key)
    key_str = ctypes.addressof((ctypes.c_char * (key_len + 1))())
    lib.kv_delete(key_str, key_len)

def kv_exit(lib):
    lib.kv_exit()
